<?php
namespace ClubAdmin\Controller;
use Think\Controller;
class CoachController extends BaseController {
public function coachList(){
        //14110100412金永春
  
        $coachmodel =M('coach'); 
        $pageSize=5;//每页显示的数据个数
        $records=$coachmodel->count(); //查询数据表中数据的总个数
        $Page = new \Think\Page($records,$pageSize);//实例化分页类，传入总数和每页显示的数量
        $list=$coachmodel->order('coach_id desc')->limit($Page->firstRow,$pageSize)->select();//进行分页数据查询
        // dump($list);
        // exit();
        //$comments=$commentsmodel->order('comments_id asc')->select();
        //$this->comments=$comments;
        $page1=$Page->show();//分页显示输出
        $this->assign(array('coach'=>$list,'page1'=>$page1));//用数组的方式赋值数据集，赋值分页输出
        $this->display();//输出模板

       
       
       /* $this->link=$Page->show();
        $this->qrcodeList=$goods->where($goodsData)->order("id desc")->limit($Page->firstRow,$pageSize)->select();*/
    }
        public function changestate($coach_id,$coach_state){
        $coachmodel=M('coach');//14110100412金永春
        $coachmodel->coach_state=($coach_state+1)%2;
        if(false!==$coachmodel->where("coach_id=$coach_id")->save()){
            //false不恒等用于精确和0进行比较
            //save保存，将值保存到数据表中。
            $this->success('状态修改成功！');
        }else{
            $this->error('状态修改失败');
        }
    }
     public function delcoach($coach_id)
    {
        
        $coachModel = M('coach');
        $coach=$coachModel->where("coach_id=$coach_id")->find();
        unlink(realpath("Public/pic/".$coach["qrpath"]));
        if($coachModel->delete($coach_id)) {
            $this->success('删除成功',U('coachList'));//14110100412金永春
        }
        else {
            $this->error('删除失败');
        }
    }
    public function addcoach($coach_name,$coach_introduce,$club_id){
       if($_FILES['file']['error']==0 ){
            $info=$this->upload();
            $name=$info["file"]["savename"];
        }
        else{
            $name="default.jpg";
        }


        $coachmodel =M('coach');
        //$coachmodel->coach_explain=$coach_explain;
        $coachmodel->coach_name=$coach_name;
        $coachmodel->club_id=$club_id;
        $coachmodel->coach_introduce=$coach_introduce;
        $coachmodel->coach_date=date('Y-m-d H:i:s');
        $coachmodel->coach_photo=$name;
        $re=$coachmodel->add();    
            if ($re>0)
            {
               $this->success('发布成功',U("Coach/coachList"));
            }
            else
            {
                $this->error('发布错误'); 
            }
    }

    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/pic/'; // 设置附件上传根目录  //  
        $upload->autoSub = false;//关闭子目录，默认true
        //$upload->savePath  =     ''; // 设置附件上传(子)目录，默认目录名为年月日
        $info   =   $upload->upload();  // 上传文件 
        if(!$info) {
            $this->error($upload->getError());    
        }
        else{
            return $info;
        }                    
    }
        public function altercoach($id){
          $coachmodel=M('coach');
          $coach=$coachmodel->where("coach_id=$id")->select();
          $this->coach=$coach;
          session('coach_id',$id);
          $this->display();
    }
     public function update(){
      $id['coach_id'] = I('session.coach_id');
      $coachmodel = M("coach");
      $data['coach_name'] = I('post.name');
  
    
      $data['coach_introduce'] = I('post.coach_introduce');
      $data['coach_attention'] = I('coach_attention');
      if ($_FILES['file']['error'] == 0) {
          $logoinfo = $this->upload();
          $pname = $logoinfo['file']['savename'];
      } else {
          $pname = "default.jpg";
      }
        $data['coach_photo'] = $pname;
        $res = $coachmodel->where($id)->save($data);
        if (false !== $res){
            $this->success('修改成功',U("Coach/coachList"));
     
        }else{
            $this->error('信息填写有误');
        }
    }
    public function coachmod(){
      $id['coach_id'] = I('session.coach_id');
      $coachmodel = M("coach");
      $data['coach_name'] = I('post.name');
      $data['coach_introduce'] = I('post.introduce');
      $data['club_id'] = I('post.club_id');
      
      if ($_FILES['file']['error'] == 0) {
          $logoinfo = $this->upload();
          $pname = $logoinfo['file']['savename'];
      } else {
          $pname = "default.jpg";
      }
     
        $data['coach_photo'] = $pname;
        $res = $coachmodel->where($id)->save($data);
        if (false !== $res){
            $this->success('修改成功',U("Coach/coachList"));
        }else{
            $this->error('信息填写有误');
        }
    }
   
    public function search($coach_id){
       
       $coachmodel =M('coach');
        $coach=$coachmodel->where("coach_id=$coach_id")->select();
        $this->coach=$coach;
        $this->display(); 
    } 
       
}
?>